import random
import sys
from optparse import OptionParser

def readFile(filename):
    """Given a filename, read the contents of that file.
    """
    try:
        f = open(filename)
    except IOError:
        print "File does not exist. Please pick another."
        sys.exit()
    line = ''
    book = []

    while 1==1:
        line = f.readline()
        if line != '':
            if line != '\n':
                book.append(line)
        else:
            break
    f.close()
    return book

def printBookPars(book, n):
    for i in range (n):
        print book[random.randrange(0,len(book))]
    return

def printListLines(book, n):
    for i in range (n):
        print book[random.randrange(0,len(book))].replace("\n","")
    return

def listLines(book, n):
    """ return a list of lines of text """
    lines = []
    for i in range (n):
        lines.append(book[random.randrange(0,len(book))].replace("\n",""))
    return lines

if __name__=='__main__':
    parser = OptionParser()
    parser.add_option("-b", "--book", dest = "book", default="cthullu.txt", help = "The name of the file you'd like to have read in.", metavar="BOOK.txt")
    parser.add_option("-p", "--paragraphs", dest = "paragraphs", default=1, help = "The number of paragraphs you'd like to generate.", metavar="#")
    parser.add_option("-l", "--list", dest = "list", default = -1, help="Indicates that you want a list of items, and how many items you would like.", metavar = "#")
    opts = parser.parse_args()
    options = opts[0]
    options.paragraphs = int(options.paragraphs)

    if options.list != -1:
        options.book = "cummings.txt"
        options.paragraphs = int(options.list)

    book = readFile(options.book)
    
    if options.list ==-1:
        printBookPars(book, options.paragraphs)
    else:
        printListLines(book, options.paragraphs)


    

